import {
  HttpStatus,
  HttpException,
  Controller,
  Get,
  Post,
  Body,
  Param,
  Logger,
  Delete,
  ParseIntPipe,
} from '@nestjs/common';
import { UserService } from './user.services';
import {
  //ApiBearerAuth,
  ApiOperation,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { CreateUserDto } from './dto/create-user.dto';
import { CreateTokenDto } from './dto/create-token.dto';
@ApiTags('user')
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}
  @Get(':email')
  @ApiOperation({ summary: 'get user by email' })
  @ApiResponse({ status: 200, description: 'User Find' })
  async getUser(@Param('email') email: string): Promise<string> {
    const user = await this.userService.getUserByEmail(email);
    console.log(user)
    return JSON.stringify(user);
  }
  @Get('id/:id')
  @ApiOperation({ summary: 'get user by email' })
  @ApiResponse({ status: 200, description: 'User Find' })
  async getUserById(@Param('id',ParseIntPipe) id: number): Promise<string> {
    const user = await this.userService.getUserById(id);
    if (!user) {
      throw new HttpException(
        `user with id ${id} not exist`,
        HttpStatus.NOT_FOUND,
      );
    }
    return JSON.stringify(user);
  }
  @Post('create')
  async create(@Body() user: CreateUserDto): Promise<string> {
    Logger.log(`create user with name:${user.name},email:${user.email} password:${user.password}`);
    const value = await this.userService.create(user);
    if (!value) {
      throw new HttpException('create user failed', HttpStatus.NOT_ACCEPTABLE);
    }
    return JSON.stringify(value);
  }

  @Delete('delete/:id')
  async delete(@Param('id') id: number) {
    Logger.log(`delete user ${id}`);
    const user = await this.userService.deleteUserById(id)
    return JSON.stringify(user);
  }
  @Get('token/:token')
  async token(@Param('token') email: string) {}
  @Post('token')
  async createToken(@Body() token: CreateTokenDto): Promise<string> {
    const value = await this.userService.createToken(token);
    if (!value) {
      throw new HttpException(
        `user ${token.email} not exist`,
        HttpStatus.NOT_FOUND,
      );
    }
    return JSON.stringify(value);
  }
  @Get('token/delete/:email')
  async deleteTokenByEmail(@Param('email') email: string) {
    const value = await this.userService.deleteTokenByEmail(email);
    Logger.log(value);
    return JSON.stringify({ status: 200, message: '' });
  }
}
